<!-- ejeje © 2007 havard@gulldahl.no -->
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/">
  <xi:include href="layout.html" />
  <head>
    <title>Innsyn</title>
    <link rel="stylesheet" type="text/css" href="/yui/datatable/datatable.css" />
    <link rel="stylesheet" type="text/css" href="/yui/container/container.css" />
    <script type="text/javascript" src="/yui/datatable/datatable-beta-min.js"></script>
    <script type="text/javascript" src="/yui/datasource/datasource-beta-min.js"></script>
    <script type="text/javascript" src="/yui/container/container-min.js"></script>
    <script type="text/javascript" src="/media/sok.js"></script>
    <script type="text/javascript" src="/media/innsyn.js"></script>
    <script type="text/javascript">
      function set(obj) {
        var q = YAHOO.util.Dom.get("q");
        q.value=obj.innerHTML;
      }

      
    </script>
    <style type="text/css">
      .request-old { background: red; }

    </style>
  </head>
  <body class="yui-skin-sam">

    <div id="innsyn-container">
    
      <table py:if="innsyn" id="innsyn-table">
        <thead>
          <tr>
            <th>id</th>
            <th>journalpost_id</th>
            <th>tittel</th>
            <th>svar_mottatt</th>
            <th>lest</th>
            <th>opprettet</th>
            <th>foresporsel_sendt</th>
            <th>purring_sendt</th>
          </tr>
        </thead>
        <tbody>
          <tr py:for="r in innsyn">
            <td>${r.id}</td>
            <td>${r.journalpost_id}</td>
            <td>${r.journalpost.organisasjon.navn}: ${r.journalpost.tittel}</td>
            <td>${r.svar_mottatt}</td>
            <td>${r.lest}</td>
            <td>${r.opprettet}</td>
            <td>${r.foresporsel_sendt}</td>
            <td>${r.purring_sendt}</td>
          </tr>
        </tbody>
      </table>

    </div>
  
    <script py:if="innsyn" type="text/javascript">

YAHOO.util.Event.addListener(window, "load", function() {

  ejejeInnsynLister = new function() {

    this.formatDate = function(elCell, oRecord, oColumn, oData) {
        if(oData.length) {
              var d = oData.split(" ")[0];
              elCell.innerHTML = d;
          }
          else {
              elCell.innerHTML = "Nei";
          }
    };

    this.formatDateColor = function(elCell, oRecord, oColumn, oData) {
      if(YAHOO.lang.isValue(oData)) {
        var d = oData.split(" ")[0];
        elCell.innerHTML = d;
        var dte = d.split("-");
        var date = Date.parse(new Date(dte[0], dte[1]-1, dte[2]));
        var now = Date.parse(Date());
        var timewindow = 1000*60*60*24*3; // three days;
        if(now-timewindow > date) { // old record
          ejeje.log("old recodrd");
          YAHOO.util.Dom.addClass(elCell, 'request-old');
        }
        
      } else {
        elCell.innerHTML = "Nei";
      }
    };

    this.formatControls = function(elCell, oRecord, oColumn, oData) {
        elCell.innerHTML = innsyn.controlHTML(oData, false);
    };
    
    var myDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("innsyn-table"));
    myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
    myDataSource.responseSchema = {
      fields: ["id","journalpost_id", "tittel", "svar_mottatt", "lest","opprettet", "foresporsel_sendt", "purring_sendt"]
    };

    var myColumnDefs = [  
  //    {key:"id", label:"ID"},
    {key:"journalpost_id", label:"Les/purr", formatter:this.formatControls},
    {key:"tittel", label:"Sak"},
    {key:"svar_mottatt", label:"Besvart",abbr:"Når forespørselen ble besvart", formatter:this.formatDate},
    {key:"lest", label:"Lest", sortable:true, formatter:"checkbox" },
    {key:"opprettet", label:"Opprettet", sortable:true, formatter:this.formatDate},
    {key:"foresporsel_sendt", label:"Sendt", abbr:"Når forespørselen om innsyn ble sendt", sortable:true, formatter:this.formatDateColor},
    {key:"purring_sendt", label:"Purret", abbr:"Når purring ble sendt", sortable:true, formatter:this.formatDate}
    ];

    var myConf = {caption:"Saker du har bedt om innsyn i",summary:"My Summary"};

    this.myDataTable = new YAHOO.widget.DataTable("innsyn-container", myColumnDefs, myDataSource, myConf);

    var recordPanel = new YAHOO.widget.Panel('recordPanel',
      { width : "600px",
        fixedcenter : true,
        visible : false,
        close : true,
        modal : false,
        constraintoviewport : false
      } );
    
    this.myDataTable.set("selectionMode","single");  
    this.myDataTable.subscribe("rowClickEvent", function(event, clickTarget) {
        try {
          if(clickTarget == undefined)
              var clickTarget = YAHOO.util.Event.getTarget(event);
            var recordId = parseInt(ejejeInnsynLister.myDataTable.getRecord(clickTarget).getData().journalpost_id, 10);
        } catch(e) {ejeje.log(e);}
        if(!YAHOO.lang.isNumber(recordId)) {// something is rotten
          debugger;
          return false;
        }
        YAHOO.util.Event.stopEvent(event);
        var targetEl = YAHOO.util.Event.getTarget(event.event);
        if(YAHOO.util.Dom.hasClass(targetEl, 'record-control')) {
          // do the action
          if(YAHOO.util.Dom.hasClass(targetEl, 'record-save'))
            return sok.save(recordId);
          else if(YAHOO.util.Dom.hasClass(targetEl, 'record-queue'))
            return sok.queue(recordId);
          else if(YAHOO.util.Dom.hasClass(targetEl, 'record-read'))
            return sok.read(recordId);

        } else {
          // show the record details
          ejeje.jsonRequest(function(record) {
            ejeje.log('got record', record)
            recordPanel.setHeader(record.tittel);

            recordPanel.setBody(sok.recordHTML(record));
            },
            '/_journalpost', 'id='+recordId);
          recordPanel.setBody("Loading record...");
          recordPanel.cfg.config.context = [clickTarget.firstChild, 'tl', 'bl']
          recordPanel.render(ejejeInnsynLister.myDataTable);
          recordPanel.show();
          return true;
        }

      }
    );
    
  }
});
    </script>
  </body>
</html>
